package com.leyou.upload.controller;

import com.leyou.upload.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @author LT
 * @version 1.0.0
 * @ClassName UploadController.java
 * @Description TODO 上传控制器
 * @createTime 2020年05月05日 09:19:00
 */
@RestController
public class UploadController {

    @Autowired
    private UploadService uploadService;

    /**
     * 本地上传图片
     *
     * @param file
     * @return
     */
    @PostMapping("/image")
    public ResponseEntity<String> uploadImage(@RequestParam("file") MultipartFile file) {
        String imgUrl = uploadService.uploadImage(file);
        return ResponseEntity.status(HttpStatus.CREATED).body(imgUrl);
    }

    /**
     * 编写阿里云OSS获取签名的处理器
     * @return
     */
    @GetMapping("/signature")
    public ResponseEntity<Map<String, String>> getOssSignature() {
        Map<String, String> signature = uploadService.getOssSignature();
        return ResponseEntity.ok(signature);
    }
}